package com.googlecode.aleverr.yafotkiclient.api;

import android.util.Log;
import org.w3c.dom.Node;

import javax.xml.xpath.*;

public class Album {

    private static final String TAG = "Album";

    private static final XPathExpression PHOTOS_EXPRESSION;
    private static final XPathExpression COVER_EXPRESSION;
    private static final XPathExpression TITLE_EXPRESSION;
    private static final XPathExpression SUMMARY_EXPRESSION;
    private static final XPathExpression ID_EXPRESSION;

    static {
        XPath xPath = XPathFactory.newInstance().newXPath();
        XPathExpression expression = null;
        try {
            expression = xPath.compile("link[@rel='photos']/@href");
        } catch (XPathExpressionException e) {
            e.printStackTrace();
        } finally {
            PHOTOS_EXPRESSION = expression;
        }
        expression = null;
        try {
            expression = xPath.compile("link[@rel='cover']/@href");
        } catch (XPathExpressionException e) {
            e.printStackTrace();
        } finally {
            COVER_EXPRESSION = expression;
        }
        expression = null;
        try {
            expression = xPath.compile("title/text()");
        } catch (XPathExpressionException e) {
            e.printStackTrace();
        } finally {
            TITLE_EXPRESSION = expression;
        }
        expression = null;
        try {
            expression = xPath.compile("summary/text()");
        } catch (XPathExpressionException e) {
            e.printStackTrace();
        } finally {
            SUMMARY_EXPRESSION = expression;
        }
        expression = null;
        try {
            expression = xPath.compile("id/text()");
        } catch (XPathExpressionException e) {
            e.printStackTrace();
        } finally {
            ID_EXPRESSION = expression;
        }
    }

    private String id;
    private String title;
    private String summary;
    private String photoCollectionAddress;
    private Photo coverPhoto;

    private Album() {
    }

    public static Album fromEntryNode(Node entryNode) {
        Album result = new Album();
        try {
            result.id = (String) ID_EXPRESSION.evaluate(entryNode, XPathConstants.STRING);
        } catch (XPathExpressionException e) {
            Log.d(TAG, e.getMessage(), e);
        }
        try {
            result.title = (String) TITLE_EXPRESSION.evaluate(entryNode, XPathConstants.STRING);
        } catch (XPathExpressionException e) {
            Log.d(TAG, e.getMessage(), e);
        }
        try {
            result.summary = (String) SUMMARY_EXPRESSION.evaluate(entryNode, XPathConstants.STRING);
        } catch (XPathExpressionException e) {
            Log.d(TAG, e.getMessage(), e);
        }
        try {
            result.photoCollectionAddress = (String) PHOTOS_EXPRESSION.evaluate(entryNode, XPathConstants.STRING);
        } catch (XPathExpressionException e) {
            Log.d(TAG, e.getMessage(), e);
        }
        try {
            String coverAddress = (String) COVER_EXPRESSION.evaluate(entryNode, XPathConstants.STRING);
            result.coverPhoto = Photo.fromUrl(coverAddress, null);
        } catch (XPathExpressionException e) {
            Log.d(TAG, e.getMessage(), e);
        }
        return result;
    }

    public String getId() {
        return id;
    }

    public String getTitle() {
        return title;
    }

    public String getSummary() {
        return summary;
    }

    public String getPhotoCollectionAddress() {
        return photoCollectionAddress;
    }

    public Photo getCoverPhoto() {
        return coverPhoto;
    }
}
